package com.dao;

import com.pojo.Student;
import com.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateTime: 2022-12-24  16:05
 * @Description: TODO
 * @Version: 1.0.0
 */
public class IStudentDAOImpl implements IStudentDAO {

    private static Connection connection;

    static {
        try {
            // 拿到连接
            connection = JdbcUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int add(Student student) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("insert into tb_student (student_name,gender,comprehensive_achievements,student_class,student_grade) values (?,?,?,?,?)");
            preparedStatement.setString(1, student.getStudentName());
            preparedStatement.setString(2, student.getGender());
            preparedStatement.setString(3, student.getComprehensiveAchievements());
            preparedStatement.setString(4, student.getStudentClass());
            preparedStatement.setString(5, student.getStudentGrade());
            return preparedStatement.execute() ? 1 : 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Student student) {
        try {
            PreparedStatement preparedStatement = null;
            preparedStatement = connection.prepareStatement("update tb_student set student_name =?,gender=?,comprehensive_achievements=?, student_class=? ,student_grade=? where id=?");
            preparedStatement.setString(1, student.getStudentName());
            preparedStatement.setString(2, student.getGender());
            preparedStatement.setString(3, student.getComprehensiveAchievements());
            preparedStatement.setString(4, student.getStudentClass());
            preparedStatement.setString(5, student.getStudentGrade());
            preparedStatement.setInt(6, student.getId());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(int id) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("delete from tb_student where id = ?");
            preparedStatement.setInt(1, id);
            return preparedStatement.execute() ? 1 : 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student findById(int id) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from tb_student where id = ?");
            preparedStatement.setInt(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setStudentName(resultSet.getString("student_name"));
                student.setGender(resultSet.getString("gender"));
                student.setComprehensiveAchievements(resultSet.getString("comprehensive_achievements"));
                student.setStudentClass(resultSet.getString("student_class"));
                student.setStudentGrade(resultSet.getString("student_grade"));
                return student;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<Student> findAllList() {
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement("select * from tb_student");
            ResultSet resultSet = preparedStatement.executeQuery();
            List<Student> students = new ArrayList<>();
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setStudentName(resultSet.getString("student_name"));
                student.setGender(resultSet.getString("gender"));
                student.setComprehensiveAchievements(resultSet.getString("comprehensive_achievements"));
                student.setStudentClass(resultSet.getString("student_class"));
                student.setStudentGrade(resultSet.getString("student_grade"));
                students.add(student);
            }
            return students;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        IStudentDAOImpl iStudentDAO = new IStudentDAOImpl();
        List<Student> students = iStudentDAO.findAllList();
        for (Student student : students) {
            System.out.println(student);
        }
    }

}
